﻿using BCCommon;
using BCCommon.Enums;
using BCData.OA.WorkflowTemplate.WorkflowTemplateStepFormCategory;
using BCDto.OA.WorkflowTemplate.WorkflowTemplateStepFormCategory;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepFormCategory;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;

namespace BCService.OA.WorkflowTemplate.WorkflowTemplateStepFormCategory
{
	public class WorkflowTemplateStepFormCategoryService : IWorkflowTemplateStepFormCategoryService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IWorkflowTemplateStepFormCategoryData workflowTemplateStepFormCategoryData;
		private readonly string key = string.Format("{0}", EntityNameConst.WorkflowTemplateStepFormCategoryEntity);

		public WorkflowTemplateStepFormCategoryService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IWorkflowTemplateStepFormCategoryData workflowTemplateStepFormCategoryData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.workflowTemplateStepFormCategoryData = workflowTemplateStepFormCategoryData;
		}

		#region 检查工作流模板步骤表单分类是否存在
		public bool Exists(long workflowTemplateStepFormCategoryId)
		{
			return this.workflowTemplateStepFormCategoryData.Exists(workflowTemplateStepFormCategoryId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加工作流模板步骤表单分类
		public WorkflowTemplateStepFormCategoryDto Add(WorkflowTemplateStepFormCategoryRequestDto requestDto,AdminDto adminDto)
		{
			var entity = requestDto.As<WorkflowTemplateStepFormCategoryEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;

			return this.workflowTemplateStepFormCategoryData.Add(entity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormCategoryDto>();
		}
		#endregion

		#region 修改工作流模板步骤表单分类
		public WorkflowTemplateStepFormCategoryDto Update(WorkflowTemplateStepFormCategoryPutDto putDto,AdminDto adminDto)
		{
			if (!this.Exists(putDto.WorkflowTemplateStepFormCategoryId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<WorkflowTemplateStepFormCategoryEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			var result = this.workflowTemplateStepFormCategoryData.Update(entity).GetAwaiter().GetResult().As<WorkflowTemplateStepFormCategoryDto>();
			string cacheKey = this.redisService.GetKey(key, entity.WorkflowTemplateStepFormCategoryId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除工作流模板步骤表单分类
		public bool Delete(long workflowTemplateStepFormCategoryId)
		{
			if (!this.Exists(workflowTemplateStepFormCategoryId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.workflowTemplateStepFormCategoryData.Delete(workflowTemplateStepFormCategoryId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, workflowTemplateStepFormCategoryId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取工作流模板步骤表单分类
		public WorkflowTemplateStepFormCategoryDto Get(long workflowTemplateStepFormCategoryId)
		{
			string cacheKey = this.redisService.GetKey(key, workflowTemplateStepFormCategoryId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workflowTemplateStepFormCategoryData.GetEntity(workflowTemplateStepFormCategoryId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<WorkflowTemplateStepFormCategoryDto>();
		}
		#endregion

		#region 获取工作流模板步骤表单分类列表
		public IEnumerable<WorkflowTemplateStepFormCategoryDto> GetList()
		{
			var result = this.redisService.TryGet(key, () =>
			{
				return this.workflowTemplateStepFormCategoryData.GetList().GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<WorkflowTemplateStepFormCategoryDto>>();
		}
		#endregion

		#region 获取工作流模板步骤表单分类分页
		public PageableList<WorkflowTemplateStepFormCategoryDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.workflowTemplateStepFormCategoryData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<WorkflowTemplateStepFormCategoryDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<WorkflowTemplateStepFormCategoryDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}